<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>JS10.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">


		<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
		<script type="text/javascript">

//创建对像的两种方式 结束
//方式1
var person = new Object();

//给属性赋值的两种方式
person.name = "kevin";
person["age"] = 25;
person.jop = "Software Engineer"

person.sayName = function() {
	alert(this.name);
};

//sayName的类型为function,因此在执行时，要加上括号。
//person.sayName();

//创建对像的方式2
var person2 = {
	name : "Annie",
	age : 25,
	sayName : function() {
		alert(this.name);
	}
};

//person2.sayName();
<!-- >>>>>>>>>>>>>>>>>>创建对像的两种方式 结束<<<<<<<<<<<<<<<<<<<<<<<<<  -->
	
	<!-- >>>>>>>>>>>>>>>>>>获得对象的方式 开始<<<<<<<<<<<<<<<<<<<<<<<<< -->
	//1.工厂模式
	function createObject(name, age, job){
	
		var o = new Object();
		o.name = name;
		o.age = age;
		o.job = job;
		o.say = function(){
			alert(this.name + " : " + this.age + " : " + this.job);
		};	
		
		return o;
	}
	
	//var person1 = createObject("kevin", 25, "Software Engineer");
	//var person2 = createObject("annie", 24, "Software Engineer");
	
	//person1.say();
	//person2.say();
	//alert(person1.constructor);
	
	//构造方法方式
	//内部执行内容
	//1.创建一个Object对象;
	//2.将构造方法的作用域指定给新创建的对象;
	//3.执行构造方法内的语句
	//4.返回创建的Object对像;
	function Person(name, age, job){
	
		this.name = name;
		this.age = age;
		this.job = job;

		this.say = function(){
			alert(this.name + " : " + this.age + " : " + this.job);
		};
	}
	
	//var person1 = new Person("kevin", 25, "Software Engineer");
	//var person2 = new Person("annie", 24, "Software Engineer");
	
	//person1.say();
	//person2.say();
	//alert(person1.constructor == Person);
	//alert(person1 instanceof Person);
	//alert(person1 instanceof Object);
	
	//将构造函数当成方法来执行
	//Person("kevin", 25, "Software Engineer");
	//window.say();
	
	var o = new Object();
	Person.call(o,"kevin", 25, "Software Engineer");
	//o.say(); 
	
	//原型模式获得对象
	function Person(){
		Person.prototype.name = "kevin";
		Person.prototype.age = 25;
		Person.prototype.job = "Software Engineer";

		Person.prototype.say = function(){
			alert(this.name + " : " + this.age + " : " + this.job);
		};
	}
	
	var person1 = new Person();
	var person2 = new Person();
	
	//person1.say();
	//alert(person1.say == person2.say);
	
	//工厂方式与原型方式的组合
	function Person(name,age,job){
		this.name = name;
		this.age = age;
		this.job = job;

		if(typeof this.say != "function")
		{
			Person.prototype.say = function(){
				alert(this.name + " : " + this.age + " : " + this.job);
			};
		}
	}
	
	var person1 = new Person("kevin", 25, "Software Engineer");
	var person2 = new Person("annie", 24, "Software Engineer");
	
/*	person1.say();
	person2.say();
	alert(person1.say == person2.say);*/
	
	<!-- >>>>>>>>>>>>>>>>>>获得对象的方式 结束<<<<<<<<<<<<<<<<<<<<<<<<< -->
		
</script>
	</head>
	<body>
	</body>
</html>
